// pages/goods/goods.js
const db = wx.cloud.database()
const DB=wx.cloud.database().collection("cart")
let delid=""
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:0,
    goods:[],
    cart:[],
    collection:[],
    colText:"",
    colIcon:"",
    inCart:false,
    inCol:false,
    openid:""
  },
  searchList(){
    let that=this;
    wx.cloud.callFunction({
      name:"searchlist",
      data:{
        goods_id:parseInt(this.data.id)
      },
      success(res){
        console.log("查找list数据成功",res);
        that.setData({
          goods:res.result.data
        })
      },
      fail(res){
        console.log("查找list数据失败",res);
      }
    })
  },
  searchCart(){
    let that=this;
    wx.cloud.callFunction({
      name:"searchcart",
      data:{
        goods_id:parseInt(this.data.id),
        openid:this.data.openid
      },
      success(res){
        console.log("查找cart数据成功",res);
        that.setData({
          cart:res.result.data
        })
        if(res.result.data.length){
          //let that=this;
          that.setData({
            inCart:true
          })
        } 
      },
      fail(res){
        console.log("查找cart数据失败",res);
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.openid)
    let that=this
    that.setData({
      openid:options.openid
    })
    console.log(options.id)
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('acceptDatafromIndex', function(data) {
      console.log(data) //这是从首页向商品详情传输的商品id
      that.setData({
        id:data.id
      })
      that.searchList();
      that.searchCart();
      that.getcollection();
    }),


    eventChannel.on('acceptDatafromClass', function(data) {
     console.log(data) //这是从分类向商品详情传输的商品id
      that.setData({
        id:data.id
      })
      that.searchList();
      that.searchCart()
      that.getcollection();
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

getcollection(){
  let that=this;
  wx.cloud.callFunction({
    name:"searchcollect",
    data:{
      goods_id:parseInt(this.data.id),
      openid:this.data.openid
    },
    success(res)
    {
      console.log("查找collection数据成功",res);
      that.setData({
        collection:res.result.data,
        
      })
      if(res.result.data.length){
        that.setData({
          colIcon:"../../image/collection.png",
          colText:"取消收藏",
          inCol:true
        })
      }else{
        that.setData({
          colIcon:"../../image/_collection.png",
          colText:"收藏",
        })
      }
    },
    fail(res){
      console.log("查找collection数据失败",res);
    }
  })
},

  tocollection(){
  
    if(this.data.inCol==false){
     wx.cloud.database().collection("collection").add({
    data:{
          goods_name:this.data.goods[0].goods_name,//商品名字
          goods_price:parseInt(this.data.goods[0].goods_price),//商品的价格
          goods_num:parseInt(this.data.goods[0].goods_num),//商品数量
          goods_logo:this.data.goods[0].goods_logo,//商品图片
          goods_id:this.data.goods[0].goods_id,//商品图片
        },
        success(res){
          console.log("收藏成功",res)
        },
        fail(res){
          console.log("收藏失败",res)
        }
      })
      wx.showToast({
        title: '成功添加收藏',
        success:'success',
        duration: 2000
      })
      this.setData({
        colIcon:"../../image/collection.png",
        colText:"取消收藏",
        inCol:true
      })
    }
    else{
      db.collection("collection").where({
        _openid:this.data.openid,
        goods_id:parseInt(this.data.id)
      }).get({
        success(res){
          console.log(res.data[0]._id);
          delid=res.data[0]._id
          db.collection("collection").doc(delid).remove({
            success(res){
              console.log("移除成功",res)
            },
            fail(res){
              console.log("移除失败",res)
            }
          })
        }
      })

      wx.showToast({
        title: '成功取消收藏',
        success:'success',
        duration: 2000
      })
      this.setData({
        colIcon:"../../image/_collection.png",
        colText:"收藏",
        inCol:false
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 添加到购物车
  addCart: function () {
    //this.searchCart();
    //console.log(this.data.goods)
    //console.log(this.data.cart)
    const _=db.command
    // 商品不在购物车中
    if(this.data.inCart==false){
      DB.add({
        data:{
          checked:false,
          goods_id:this.data.goods[0].goods_id,
          goods_price:this.data.goods[0].goods_price,
          goods_name:this.data.goods[0].goods_name,
          goods_logo:this.data.goods[0].goods_logo,
          goods_num:1
        },
        success(res){
          console.log("添加成功",res)
        },
        fail(res){
          console.log("添加失败",res)
        }
      })
      let that=this
      that.setData({
        inCart:true
      })
    } else{// 商品在购物车中 商品数目+1
      db.collection("cart").where({
        goods_id:parseInt(this.data.id),_openid:this.data.openid}).update({
        data: {
          goods_num: _.inc(1)
        },
        success: function(res) {
          console.log(res.data)
        }
      })
    }
    wx.showToast({
      title: '成功加入购物车',
      icon:'success',
      duration: 2000
    })
  },
  /*
  立即支付
  先加到购物车 checked改为true
  再跳转到支付页面
  */
  pay:function () {
    if(this.data.cart.length==0){
      DB.add({
        data:{
          checked:true,
          goods_id:this.data.goods[0].goods_id,
          goods_price:this.data.goods[0].goods_price,
          goods_name:this.data.goods[0].goods_name,
          goods_logo:this.data.goods[0].goods_logo,
          goods_num:1
        },
        success(res){
          console.log("添加成功",res)
        },
        fail(res){
          console.log("添加失败",res)
        }
      })
    }else{
      db.collection("cart").doc(this.data.cart[0]._id).update({
        data: {
          checked: true
        },
        success: function(res) {
          console.log(res.data)
        },
        fail(res){
          console.log("更新失败",res)
        }
      })
    }
    wx.navigateTo({
      url: '../pay/pay',
      success: function(res){
        res.eventChannel.emit('acceptDatafromGoods',{data:this.data.goods})
      }
    })
  }
})